package com.bionic.banking.dao.classes;

import com.bionic.banking.core.entity.BankAccount;
import com.bionic.banking.dao.IBankAccountDao;
import com.bionic.banking.dao.connection.DBQueryManager;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BankAccountDaoImp implements IBankAccountDao {

    protected final String SQL_SELECT = "SELECT * FROM " + getTableName();
    protected final String SQL_INSERT = "INSERT INTO " + getTableName()
      + "(currency_id, customer_id,account_type,description,begin_balance,balance, credit_line, createdon) "
      + " VALUES(?,?,?,?,?,?,?,?)";
    protected final String SQL_UPDATE = "UPDATE " + getTableName()
            + " SET currency_id=?, account_type=?,description=?,balance=? ";
    protected final String SQL_DELETE = "DELETE FROM " + getTableName();
    protected final String SQL_WHERE = " WHERE " + getKeyColumn() + "=?";
    protected static final int COLUMN_ACCOUNTID = 1;
    protected static final int COLUMN_CURRENCY = 2;
    protected static final int COLUMN_CUSTOMERID = 3;
    protected static final int COLUMN_ACCOUNTTYPE = 4;
    protected static final int COLUMN_DESCRIPTION = 5;
    protected static final int COLUMN_BALANCE = 6;
    protected static final int COLUMN_CREDIT = 7;
    protected static final int COLUMN_BEGINBALANCE = 8;
    protected static final int COLUMN_DATECREATEDON = 9;
    protected int maxRows;

    @Override
    public String getTableName() {
        return "bank_account";
    }

    @Override
    public String getKeyColumn() {
        return "ACCOUNT_ID";
    }

    @Override
    public DBQueryManager<BankAccount> getDBManager() {
        return new DBQueryManager<>(this);
    }

    @Override
    public BankAccount populateDto(ResultSet rs) throws SQLException {
        BankAccount dto = new BankAccount();
        dto.setAccountId(rs.getInt(COLUMN_ACCOUNTID));
        dto.setCurrencyId(rs.getInt(COLUMN_CURRENCY));
        dto.setCustomerId(rs.getInt(COLUMN_CUSTOMERID));
        dto.setAccountType(rs.getString(COLUMN_ACCOUNTTYPE));
        dto.setDescription(rs.getString(COLUMN_DESCRIPTION));
        dto.setBalance(new BigInteger(rs.getString(COLUMN_BALANCE)));
        dto.setCreditLine(new BigInteger(rs.getString(COLUMN_CREDIT)));
        dto.setBeginBalance(new BigInteger(rs.getString(COLUMN_BEGINBALANCE)));
        dto.setCreatedon(rs.getTimestamp(COLUMN_DATECREATEDON));
        return dto;
    }

    @Override
    public Integer create(BankAccount account) throws SQLException {
        Object[] params = new Object[]{
            account.getCurrencyId(),
            account.getCustomerId(),
            account.getAccountType(),
            account.getDescription(),
            account.getBeginBalance(),
            account.getBalance(),
            account.getCreditLine(),
            account.getCreatedon()
        };
        Integer newAccountId = getDBManager().executeCreateQuery(SQL_INSERT, params);
        account.setAccountId(newAccountId); //update with new generated PK in DB 
        return newAccountId;
    }

    @Override
    public boolean update(BankAccount account) throws SQLException {
        final String query = SQL_UPDATE + SQL_WHERE;
        Object[] params = new Object[]{
            account.getCurrencyId(),
            account.getAccountType(),
            account.getDescription(),
            account.getBalance(),
            account.getAccountId()
        };
        return getDBManager().executeSaveQuery(query, params);
    }

    @Override
    public void remove(int id) throws SQLException {
        final String query = SQL_DELETE + SQL_WHERE;
        getDBManager().executeRemoveQuery(query, id);
    }

    @Override
    public BankAccount findById(int id) throws SQLException {
        String query = SQL_SELECT + SQL_WHERE;
        Object[] params = new Object[]{new Integer(id)};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public List<BankAccount> findByCustomerId(int cutomeid) throws SQLException {
        String query = SQL_SELECT + " WHERE CUSTOMER_ID = ?";
        Object[] params = new Object[]{cutomeid};

        List<BankAccount> list = getDBManager().executeSelectQuery(query, params);
        return list;
    }

    @Override
    public List<BankAccount> findAll() throws SQLException {
        String query = SQL_SELECT;
        return getDBManager().executeSelectQuery(query, null);
    }

    @Override
    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    @Override
    public int getMaxRows() {
        return maxRows;
    }
}
